<?php
    
    class CDbms
    {
		var $m_hDbms;	//dbms abstract driver handler
		
		/* construct object of the class */
		/* return reference to created object */
		static function &GetInstance()
		{
			global $g_hObjects;
			static $m_hApp = NULL;
			
			if(!isset($m_hApp))
			{
				$m_hApp = new CDbms();
				
				$g_hObjects[] = $m_hApp;
				
				$m_hApp->OnInit();
			}
			
			return $m_hApp;
		}
		
		function OnInit()
		{
			$this->m_hDbms = NULL;
		}
		
		function OnDestroy()
		{
		}
		
		//connect DSN
		function ConnectDsn($szUrl)
		{
			$hDsn = @parse_url($szUrl);
			
			
			if($hDsn['scheme'] == "pgsql")
			{
				$this->m_hDbms = CPgSql::GetInstance();
				
				$ret=$this->m_hDbms->Connect($hDsn['host'],$hDsn['port'],$hDsn['user'],$hDsn['pass'],trim($hDsn['path'],'/'));
				
				if($ret == false)
				{
					SetError("Can't connect to dbms: ".$this->GetErrorMsg(),true);
					return false;
				}
						
				//setting client encoding
				$this->SetEncoding(ENCODING);
				
				return true;
			}
			

			if($hDsn['scheme'] == "mysql")
			{
				$this->m_hDbms = CMySql::GetInstance();	

				$ret=$this->m_hDbms->Connect($hDsn['host'],$hDsn['port'],$hDsn['user'],$hDsn['pass'],trim($hDsn['path'],'/'));
				
				if($ret == false)
				{
					throw new Exception("Nie mogę połączyć się z bazą danych: ".$this->GetErrorMsg(),true);
					return false;
				}
						
				//ustawiam kodowanie
				$this->SetEncoding(ENCODING);
				
				return true;
			}
		}
		
		/* close connection to dbms */
		function Close()
		{
			$this->m_hDbms->Close();
		}
		
		/* get error msg */
		function GetErrorMsg()
		{
			return $this->m_hDbms->GetErrorMsg();
		}
		
		/* send query to dbms */
		function Query($szQuery)
		{
			if($this->m_hDbms->Query($szQuery) == false)
			{
				throw new Exception('zapytanie ['.$szQuery.'] zakonczone bledem: '.$this->GetErrorMsg());
				return false;
			}
			
			return true;
		}
		
		/* returnquery result handler */
		function GetQueryHandler()
		{
			return $this->m_hDbms->GetQueryHandler();
		}
		
		/* free result */
		function FreeResult()
		{
			$this->m_hDbms->FreeResult();
		}
		
		/* set client encoding */
		function SetEncoding($szEncoding)
		{
			$this->m_hDbms->SetEncoding($szEncoding);
		}
		
		/* returns the number of rows in a result */
		function GetResultCount()
		{
			return $this->m_hDbms->GetResultCount();
		}
		
		/* get result as row */
		function GetResultAsRow()
		{
			return $this->m_hDbms->GetResultAsRow();
		}
		
		/* get result as array */
		function GetResultAsArray()
		{
			return $this->m_hDbms->GetResultAsArray();
		}
		
		/* get query result */
		function GetResult()
		{
			return $this->m_hDbms->GetResult();
		}
	
    }
    
?>